package autoteam;

import java.util.List;

import db.ColumnData;
import db.DataAccessObject;
import db.DBRecord;
import db.SQLDataTypes;
import db.TableField;

public class Coach
{
	DataAccessObject dao;

	TableField Name = new TableField("Name", SQLDataTypes.VARCHAR);
	TableField PicturePath = new TableField("PicturePath", SQLDataTypes.VARCHAR);
	TableField Biography = new TableField("Biography", SQLDataTypes.VARCHAR);
	TableField KeyName = new TableField("KeyName", SQLDataTypes.VARCHAR);

	TableField TeamSize = new TableField("TeamSize", SQLDataTypes.VARCHAR);
	TableField RowCounter = new TableField("RowCounter", SQLDataTypes.VARCHAR);

	public Coach()
	{
		//
	}

	public Coach(int i)
	{
		// Creates a new Coach List
		dao = new DataAccessObject();
		 
		dao.createTable("RowC", KeyName, TeamSize, RowCounter);
		// dao.createTable( "Coaches", Name, PicturePath, Biography );
		List<DBRecord> recordList = dao.getAllRecords("RowC");
		// Check if RowC make a new Team
		if (recordList.size() == i)
		{
			String teamname = "Team";
			dao.addRecord("RowC", new ColumnData("KeyName", teamname),
					new ColumnData("TeamSize", "0"), new ColumnData(
							"RowCounter", "0"));
		}
		 
	}

	public void addCoach(String names, String pictures, String biographys)
	{
		 
		// Creating a record of each coach
		dao.addRecord("Coaches", new ColumnData("Name", names), new ColumnData(
				"PicturePath", pictures), new ColumnData("Biography",
				biographys));

		 

	}

	public int getId(String name)
	{
		int number;
		 
		ColumnData userName = new ColumnData("lastname", name);
		DBRecord tempRecord = dao.getRecord("Coaches", userName);
		String temp = tempRecord.getFieldValue("id").toString();
		number = java.lang.Integer.parseInt(temp);
		 
		return number;
	}

	public String getPicturePath(String coach)
	{
		String path;
		 
		ColumnData userName = new ColumnData("lastname", coach);
		DBRecord tempRecord = dao.getRecord("coaches", userName);
		path = (String) tempRecord.getFieldValue("PicturePath");
		System.out.println("PATH: " + path);
		 
		return path;
	}

	public void setPicturePath(String coachLastname, String path)
	{
		ColumnData idCol = new ColumnData("lastname", coachLastname);
		ColumnData[] identifiers = new ColumnData[1];
		identifiers[0] = idCol;
		ColumnData pictures = new ColumnData("PicturePath", path);
		ColumnData[] changes = new ColumnData[1];
		changes[0] = pictures;

		 
		dao.updateRecord("Coaches", identifiers, changes);
		 

	}

	public String getBiography(String Coach)
	{
		String bio;
		 
		ColumnData userName = new ColumnData("lastname", Coach);
		DBRecord tempRecord = dao.getRecord("Coaches", userName);
		bio = (String) tempRecord.getFieldValue("Biography");
		 
		return bio;
	}

	public void setBiography(String Coach, String biography)
	{
		ColumnData idCol = new ColumnData("lastname", Coach);
		ColumnData[] identifiers = new ColumnData[1];
		identifiers[0] = idCol;
		ColumnData bio = new ColumnData("Biography", biography);
		ColumnData[] changes = new ColumnData[1];
		changes[0] = bio;
		 
		dao.updateRecord("Coaches", identifiers, changes);
		 
	}

	public void deleteCoach(String name)
	{
		 
		ColumnData idCol = new ColumnData("lastname", name);
		dao.deleteRecord("Coaches", idCol);
		 
	}

	public void deleteCoach(int idnumber)
	{
		 
		ColumnData idCol = new ColumnData("id", idnumber);
		dao.deleteRecord("Coaches", idCol);
		 
	}

	public void setCoach(int Id, String newname)
	{
		ColumnData idCol = new ColumnData("id", Id);
		ColumnData[] identifiers = new ColumnData[1];
		identifiers[0] = idCol;
		ColumnData userName = new ColumnData("lastname", newname);
		ColumnData[] changes = new ColumnData[1];
		changes[0] = userName;
		 
		dao.updateRecord("Coaches", identifiers, changes);
		 
	}

	private void printRecords()
	{
		 
		List<DBRecord> recordList = dao.getAllRecords("Coaches");
		for (DBRecord r : recordList)
			System.out.println("Name : "
					+ r.getFieldValue("firstname").toString()
					+ " PicturePath: "
					+ r.getFieldValue("PicturePath").toString()
					+ " Biography: " + r.getFieldValue("Biography").toString());
		 
	}

	public void deleteRecords()
	{
		int num;
		 
		List<DBRecord> recordList = dao.getAllRecords("Coaches");
		for (DBRecord r : recordList)
		{
			String temp = r.getFieldValue("id").toString();
			num = java.lang.Integer.parseInt(temp);
			ColumnData idCol = new ColumnData("id", num);
			dao.deleteRecord("Coaches", idCol);
		}
		dao.dropTable("Coaches");
		dao.createTable("Coaches", Name, PicturePath, Biography);
		 
		resetTableValues();
	}

	private void resetTableValues()
	{
		 
		ColumnData idCol = new ColumnData("id", 0);
		ColumnData[] identifiers = new ColumnData[1];
		identifiers[0] = idCol;
		ColumnData sizes = new ColumnData("TeamSize", "0");
		ColumnData count = new ColumnData("RowCounter", "0");
		ColumnData[] changes = new ColumnData[2];
		changes[0] = sizes;
		changes[1] = count;
		dao.updateRecord("RowC", identifiers, changes);
		 
	}

	public void setTeamSize(int size)
	{
		String temp = java.lang.String.valueOf(size);
		ColumnData idCol = new ColumnData("id", 0);
		ColumnData[] identifiers = new ColumnData[1];
		identifiers[0] = idCol;
		ColumnData sizes = new ColumnData("TeamSize", temp);
		ColumnData[] changes = new ColumnData[1];
		changes[0] = sizes;
		 
		dao.updateRecord("RowC", identifiers, changes);
		 
	}

	public int getTeamSize()
	{
		int ret;
		 
		ColumnData sizeID = new ColumnData("id", 0);
		DBRecord tempRecord = dao.getRecord("RowC", sizeID);
		String temp = tempRecord.getFieldValue("TeamSize").toString();
		ret = java.lang.Integer.parseInt(temp);
		 
		return ret;
	}

	public void setCounter(int size)
	{
		String temp = java.lang.String.valueOf(size);
		ColumnData idCol = new ColumnData("id", 0);
		ColumnData[] identifiers = new ColumnData[1];
		identifiers[0] = idCol;
		ColumnData sizes = new ColumnData("RowCounter", temp);
		ColumnData[] changes = new ColumnData[1];
		changes[0] = sizes;
		 
		dao.updateRecord("RowC", identifiers, changes);
		 
	}

	public int getCounter()
	{
		int ret;
		 
		ColumnData sizeID = new ColumnData("id", 0);
		DBRecord tempRecord = dao.getRecord("RowC", sizeID);
		String temp = tempRecord.getFieldValue("RowCounter").toString();
		ret = java.lang.Integer.parseInt(temp);
		 
		return ret;
	}

	public static void main(String[] args)
	{
		Coach n = new Coach();
		n.addCoach("Juan", "Newsss", "juan bio");
		n.printRecords();
		n.deleteRecords();
	}
}
